#include "EditorToolTipWindowView.h"
#include "EditorToolTipWindowViewModel.h"

#include "Framework/Editor.h"
#include "Theme/EditorTheme.h"

#include "Widgets/TextWidget.h"
#include "Widgets/RectangleWidget.h"
#include "Widgets/Window.h"

void REditorToolTipWindowView::BindViewModel(TSharedObjectPtr<REditorToolTipWindowViewModel> InViewModel)
{
    ViewModel=InViewModel;

    Window=NewObject<RWindow>(this);
    Window->HideSystemBorder();
    Window->GetRootWidget()->Fill=false;

    // Window->Width=[this]()
    // {
    //     return (int64_t)ViewModel->GetToolTipSize().X;
    // };
    // Window->Height=[this]()
    // {
    //     return (int64_t)ViewModel->GetToolTipSize().Y;
    // };
    // Window->X=[this]()
    // {
    //     return (int64_t)ViewModel->GetToolTipPosition().X;
    // };
    // Window->Y=[this]()
    // {
    //     return (int64_t)ViewModel->GetToolTipPosition().Y;
    // };
    Window->Visible=[this]()
    {
        return ViewModel->IsVisible();
    };

    auto Background=NewWidget<RRectangleWidget>(Window->GetRootWidget());
    Background->Anchor.Fill(Window->GetRootWidget());
    Background->FillColor=REditor::Get()->CurrentTheme->ForegroundColor;
    Background->Radius=4;
    Background->BorderSize=1;
    Background->BorderColor=
        REditor::Get()->CurrentTheme->SpliterColor;

    auto Text=NewWidget<RTextWidget>(Background);
    Text->Anchor.Fill(Background);
    Text->Text=[this]()
    {
        return ViewModel->GetContent();
    };
    Text->VerticalAlignment=ETextVerticalAlignment::Center;
    Text->HorizontalAlignment=ETextHorizontalAlignment::Center;
    Text->FontSize=[this]()
    {
        return ViewModel->GetFontSize();
    };
    Text->Font=[this]()
    {
        return ViewModel->GetFont();
    };

    UpdateWindowPositionAndSize();
}

void REditorToolTipWindowView::Tick(float DeltaSeconds)
{
    UpdateWindowPositionAndSize();
}

void REditorToolTipWindowView::UpdateWindowPositionAndSize()
{
    Window->SetWindowPosition(ViewModel->GetToolTipPosition());
    Window->SetWindowSize(ViewModel->GetToolTipSize());
}
